<?php

class PluginController extends Zend_Controller_Action
{
    function preDispatch()
    {
		$this->_helper->layout->disableLayout();
    }
	
	function testAction()
	{
		if(3 != 3 && 3 != 2)
		{
			echo "A";
		}else{
			echo "B";
		}
		
		echo "End.";
		die;
	}
	
	function test5Action()
	{
		die;
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$select = $dealers_db->select();
		$select->from("b_logs_contract", "*");
		$select->where("value < 0");
		$rows = $dealers_db->fetchAll($select);
		
		foreach($rows as $row)
		{
			$set = array(
				"contract_id" => $row['contract_id'],
				"plan_date" => $row['ctime'],
				"plan_value" => $row['value'],
				"tips" => "系统批量刷入历史数据 原始b_logs_contract的lid=".$row['lid'],
				"status" => 1,
				"confirmed_time" => $row['ctime'],
				"ctime" => $row['ctime']
			);
			
			$dealers_db->insert("b_logs_contract_pay_plan", $set);
		}
		
		echo "End.";
		die;
	}
	
	function test6Action()
	{
		die;
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$select = $dealers_db->select();
		$select->from("b_logs_contract", "*");
		$rows = $dealers_db->fetchAll($select);
		
		foreach($rows as $row)
		{
			$set = array(
				"contract_id" => $row['contract_id'],
				"plan_date" => $row['ctime'],
				"plan_value" => $row['value'],
				"tips" => $row['tips']." [系统批处理lid=".$row['lid']."]",
				"invoice" => $row['invoice'],
				"emp_id" => $row['emp_id'],
				"status" => 1,
				"confirmed_time" => $row['ctime'],
				"ctime" => $row['ctime']
			);
			
			$dealers_db->insert("b_logs_contract_pay_plan", $set);
		}
		
		echo "End.";
		die;
	}
	
	function test3Action()
	{
		echo date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
		die;
	}
	
	
	function errorTypesAction()
	{
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
		
		$select = $maozhua_wb->select();
		$select->from("logs_task", array("error_msg"));
		$select->where("ctime >= ?", "2019-07-01 00:00:00");
		$select->where("ctime < ?", "2019-08-01 00:00:00");
		$select->where("status = ?", 0);
		$rows = $maozhua_wb->fetchAll($select);
		
		$results = array(
			"用户删除任务" => 0,
			"多次打开新发文页出错，可能是不存在此车系或网站繁忙" => 0,
			"多次发文失败，汽车之家没有返回错误原因，可能是网站繁忙" => 0,
			"最大优惠金额参数不正确" => 0,
			"您的文章可能包含其他网站链接、其他竞品消息、非汽车之家的联系方式，请修改后再提交" => 0,
			"读取老文章时出错，可能是经销商ID填错或老文章已不存在" => 0,
			"您选择的优惠幅度超出了当前的限制金额" => 0,
			"编辑时间过长，已超过预期发布时间" => 0,
			"贵公司当天针对该车系的优惠促销模板文章过多，请明天再发布" => 0,
			"账号登录失败，请稍候重试" => 0,
			"新文章标题获取失败，可能是网站繁忙" => 0,
			"读取老文章json时出错，可能是网页或服务器繁忙" => 0,
			"加装车和原始车型不能同时发布, 进入编辑详情页时出错，可能是网站或服务器繁忙" => 0,
			"请指定至少一个车型后再发布" => 0,
			"本周的推荐名额已经耗尽，无法完成推荐" => 0,
			"您没有编辑文章或发文的权限，请登录网站检查" => 0,
			"多次打开老文章时出错，可能是网站或服务器繁忙" => 0,
			"新发文页加载不完整" => 0,
			"多次打开新发文页超时，可能是网站或服务器繁忙" => 0,
			"打开发文页出错，可能是网站或服务器繁忙，请稍候重试" => 0,
			"进入编辑详情页时出错，可能是网站或服务器繁忙" => 0,
			"文章保存失败" => 0,
			"新文章标题获取失败，所有标题都包含屏蔽关键字" => 0,
			"发文失败" => 0
		);
		
		foreach($rows as $row)
		{
			if(strpos($row['error_msg'], '用户删除任务') !== false){
				$results['用户删除任务'] += 1;
			}elseif(strpos($row['error_msg'], '多次打开新发文页出错，可能是不存在此车系或网站繁忙') !== false){
				$results['多次打开新发文页出错，可能是不存在此车系或网站繁忙'] += 1;
			}elseif(strpos($row['error_msg'], '多次发文失败，汽车之家没有返回错误原因，可能是网站繁忙') !== false){
				$results['多次发文失败，汽车之家没有返回错误原因，可能是网站繁忙'] += 1;
			}elseif(strpos($row['error_msg'], '最大优惠金额参数不正确') !== false){
				$results['最大优惠金额参数不正确'] += 1;
			}elseif(strpos($row['error_msg'], '您的文章可能包含其他网站链接、其他竞品消息、非汽车之家的联系方式，请修改后再提交') !== false){
				$results['您的文章可能包含其他网站链接、其他竞品消息、非汽车之家的联系方式，请修改后再提交'] += 1;
			}elseif(strpos($row['error_msg'], '读取老文章时出错，可能是经销商ID填错或老文章已不存在') !== false){
				$results['读取老文章时出错，可能是经销商ID填错或老文章已不存在'] += 1;
			}elseif(strpos($row['error_msg'], '您选择的优惠幅度超出了当前的限制金额') !== false){
				$results['您选择的优惠幅度超出了当前的限制金额'] += 1;
			}elseif(strpos($row['error_msg'], '编辑时间过长，已超过预期发布时间') !== false){
				$results['编辑时间过长，已超过预期发布时间'] += 1;
			}elseif(strpos($row['error_msg'], '贵公司当天针对该车系的优惠促销模板文章过多，请明天再发布') !== false){
				$results['贵公司当天针对该车系的优惠促销模板文章过多，请明天再发布'] += 1;
			}elseif(strpos($row['error_msg'], '账号登录失败，请稍候重试') !== false){
				$results['账号登录失败，请稍候重试'] += 1;
			}elseif(strpos($row['error_msg'], '新文章标题获取失败，可能是网站繁忙') !== false){
				$results['新文章标题获取失败，可能是网站繁忙'] += 1;
			}elseif(strpos($row['error_msg'], '读取老文章json时出错，可能是网页或服务器繁忙') !== false){
				$results['读取老文章json时出错，可能是网页或服务器繁忙'] += 1;
			}elseif(strpos($row['error_msg'], '加装车和原始车型不能同时发布, 进入编辑详情页时出错，可能是网站或服务器繁忙') !== false){
				$results['加装车和原始车型不能同时发布, 进入编辑详情页时出错，可能是网站或服务器繁忙'] += 1;
			}elseif(strpos($row['error_msg'], '请指定至少一个车型后再发布') !== false){
				$results['请指定至少一个车型后再发布'] += 1;
			}elseif(strpos($row['error_msg'], '本周的推荐名额已经耗尽，无法完成推荐') !== false){
				$results['本周的推荐名额已经耗尽，无法完成推荐'] += 1;
			}elseif(strpos($row['error_msg'], '您没有编辑文章或发文的权限，请登录网站检查') !== false){
				$results['您没有编辑文章或发文的权限，请登录网站检查'] += 1;
			}elseif(strpos($row['error_msg'], '多次打开老文章时出错，可能是网站或服务器繁忙') !== false){
				$results['多次打开老文章时出错，可能是网站或服务器繁忙'] += 1;
			}elseif(strpos($row['error_msg'], '新发文页加载不完整') !== false){
				$results['新发文页加载不完整'] += 1;
			}elseif(strpos($row['error_msg'], '多次打开新发文页超时，可能是网站或服务器繁忙') !== false){
				$results['多次打开新发文页超时，可能是网站或服务器繁忙'] += 1;
			}elseif(strpos($row['error_msg'], '打开发文页出错，可能是网站或服务器繁忙，请稍候重试') !== false){
				$results['打开发文页出错，可能是网站或服务器繁忙，请稍候重试'] += 1;
			}elseif(strpos($row['error_msg'], '进入编辑详情页时出错，可能是网站或服务器繁忙') !== false){
				$results['进入编辑详情页时出错，可能是网站或服务器繁忙'] += 1;
			}elseif(strpos($row['error_msg'], '最大优惠金额参数不正确') !== false){
				$results['最大优惠金额参数不正确'] += 1;
			}elseif($row['error_msg'] == 'Error: 文章保存失败'){
				$results['文章保存失败'] += 1;
			}elseif(strpos($row['error_msg'], '新文章标题获取失败，所有标题都包含屏蔽关键字') !== false){
				$results['新文章标题获取失败，所有标题都包含屏蔽关键字'] += 1;
			}elseif($row['error_msg'] == 'Error: 发文失败'){
				$results['发文失败'] += 1;
			}else{
				//echo $row['error_msg']."<br>";
			}
		}
		
		arsort($results);
		
		foreach($results as $key => $val)
		{
			echo $key." ".$val."<br>";
		}
		
		$select = $maozhua_wb->select();
		$select->from("logs_task", array("count(*) as ct"));
		$select->where("ctime >= ?", "2019-07-01 00:00:00");
		$select->where("ctime < ?", "2019-08-01 00:00:00");
		$row = $maozhua_wb->fetchRow($select);
		
		echo "<br>".$row['ct'];
		
		die;
	}
	
	function dailyCountAction()
	{
		$hxb_db = Zend_Registry::get('hxb_db');
		
		$result = array();
		
		for($n=0;$n<8;$n++)
		{
			$d = date("Y-m-d", mktime(1,1,1,date("m"),date("d")-$n,date("Y")));
			
			$sql = "select count(distinct u.dealerid ) as 'ct' from hxb_db.t_device_log log left join hxb_db.t_user u on log.userId = u.id where left(log.createTime ,10) = '".$d."' ";
			
			$row = $hxb_db->query($sql)->fetchRow();
			
			var_dump($row);die;
		}
		
		$this->view->data = $result;
	}
	
	function test2Action()
	{
		var_dump(strtotime(date("Y-m-d")));
		
		die;
	}
	
	function batchDealerToClientAction()
	{
		die;
		
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$t = date("Y-m-d H:i:s");
		
		for($n=0;$n<400;$n++)
		{
			$select = $dealers_db->select();
			$select->from("dealers_db.t_dealer", array("companyName", "provinceId", "provinceName", "cityId", "cityName", "countyId", "countyName", "address", "tel", "id"));
			$select->limit(100, 100 * $n);
			$rows = $dealers_db->fetchAll($select);

			foreach($rows as $row)
			{
				$set = array(
					"client_name" => $row['companyName'],
					"province_id" => $row['provinceId'],
					"province_name" => $row['provinceName'],
					"city_id" => $row['cityId'],
					"city_name" => $row['cityName'],
					"county_id" => $row['countyId'],
					"county_name" => $row['countyName'],
					"address" => $row['address'],
					"tel" => $row['tel'],
					"is_dealer" => $row['id'],
					"status" => 2,
					"ctime" => $t
				);
				
				$dealers_db->insert("b_client", $set);
			}
		}
		
		if(1)
		{
			$select = $dealers_db->select();
			$select->from("t_dealer_hxb", array("companyName", "provinceId", "provinceName", "cityId", "cityName", "countyId", "countyName", "address", "tel", "id"));
			$select->where("authState = ?", 1);
			$rows = $dealers_db->fetchAll($select);

			foreach($rows as $row)
			{
				$set = array(
					"client_name" => $row['companyName'],
					"province_id" => $row['provinceId'],
					"province_name" => $row['provinceName'],
					"city_id" => $row['cityId'],
					"city_name" => $row['cityName'],
					"county_id" => $row['countyId'],
					"county_name" => $row['countyName'],
					"address" => $row['address'],
					"tel" => $row['tel'],
					"is_dealer" => $row['id'],
					"status" => 2,
					"ctime" => $t
				);
				
				$dealers_db->insert("b_client", $set);
			}
		}
		
		echo "End.";
		die;
	}
	
	function updatePwdAction()
	{
		die;
		$hxb_db = Zend_Registry::get('hxb_db');
		$select = $hxb_db->select();
			$select->from("auth_emp", array("id", "username"));
			$select->where("id >= ?", 95);
			$rows = $hxb_db->fetchAll($select);

			foreach($rows as $row)
			{
				$set = array(
					"password" => md5(strtolower($row['username'])."123456")
				);
				
				$where = "id=".$row['id'];
				
				$hxb_db->update("auth_emp", $set, $where);
			}
			
			echo "End.";
			die;
	}
	
	function mailTestAction()
	{
		$to = 'mark560@icloud.com';
		$body = iconv("utf-8","gb2312//IGNORE",'<div>今天是星期四<br>明天是星期五666</div>');
		$subjet = iconv("utf-8","gb2312//IGNORE",'今天是星期四');
		$mailer = new Sys_Mail();
		$result = $mailer->sendMail($to,$subjet,$body);
		var_dump($result);
		die;
		
		$to = 'mark560@qq.com';
		$body = iconv("utf-8","gb2312//IGNORE",'<div>今天是星期四<br>明天是星期五555</div>');
		$subjet = iconv("utf-8","gb2312//IGNORE",'今天是星期四');
		$mailer = new Sys_Mail();
		$result = $mailer->sendMail($to,$subjet,$body);
		var_dump($result);
		
		$to = 'chenpeng@xiongmaocar.com';
		$body = iconv("utf-8","gb2312//IGNORE",'<div>今天是星期四<br>明天是星期五555</div>');
		$subjet = iconv("utf-8","gb2312//IGNORE",'今天是星期四');
		$mailer = new Sys_Mail();
		$result = $mailer->sendMail($to,$subjet,$body);
		var_dump($result);
		
		echo "End.";
		die;
	}
	
	function mailTest2Action()
	{
		$to = 'mark560@icloud.com';
		$body = iconv("utf-8","gb2312//IGNORE","<br>客户全称：北京庞大豪庭汽车销售服务有限公司<br>品牌：吉利汽车<br>厂商：吉利汽车<br>渠道：吉利汽车, 吉利G网<br>产品：代运营 > 2018 Q1 > 汽车之家 > A类城市 > 季度<br>数量：1<br>服务期限：2018-03-01 至 2018-03-31<br><br>【托管平台】<br>汽车之家 2018-03-01 至 2018-03-15<br>易车 2018-03-01 至 2018-03-22<br>爱卡 2018-03-07 至 2018-03-31<br><br><a href='http://chezhi:8888/login/confirm-contract-sku/token/2815f6b98b7a1fc00fc6bbb6d86583c410d86af7'>【点击此处确认上线】</a><br>");
		$subjet = iconv("utf-8","gb2312//IGNORE",'【商务系统】北京庞大豪庭汽车销售服务有限公司代运营上线通知，合同编号：CZ2018030803');
		$mailer = new Sys_Mail();
		$result = $mailer->sendMail($to,$subjet,$body);
		var_dump($result);
		
		$to = 'mark560@qq.com';
		$body = iconv("utf-8","gb2312//IGNORE","<br>客户全称：北京庞大豪庭汽车销售服务有限公司<br>品牌：吉利汽车<br>厂商：吉利汽车<br>渠道：吉利汽车, 吉利G网<br>产品：代运营 > 2018 Q1 > 汽车之家 > A类城市 > 季度<br>数量：1<br>服务期限：2018-03-01 至 2018-03-31<br><br>【托管平台】<br>汽车之家 2018-03-01 至 2018-03-15<br>易车 2018-03-01 至 2018-03-22<br>爱卡 2018-03-07 至 2018-03-31<br>");
		$subjet = iconv("utf-8","gb2312//IGNORE",'【商务系统】北京庞大豪庭汽车销售服务有限公司代运营上线通知，合同编号：CZ2018030803');
		$mailer = new Sys_Mail();
		$result = $mailer->sendMail($to,$subjet,$body);
		var_dump($result);
		
		$to = 'chenpeng@xiongmaocar.com';
		$body = iconv("utf-8","gb2312//IGNORE","<br>客户全称：北京庞大豪庭汽车销售服务有限公司<br>品牌：吉利汽车<br>厂商：吉利汽车<br>渠道：吉利汽车, 吉利G网<br>产品：汇销宝 > 2018 Q1 > 会员版 > 会员 > 半年度 > A类城市<br>数量：1<br>服务期限：2018-03-01 至 2018-03-16<br><br>【托管平台】<br>汽车之家 2018-03-01 至 2018-03-08<br>易车 2018-03-07 至 2018-03-16<br><br><a href='http://chezhi:8888/login/confirm-contract-sku/token/eadc1dd8fc279583d5552700ae5d248e3fa123bd'>【点击此处确认上线】</a><br>");
		$subjet = iconv("utf-8","gb2312//IGNORE",'【商务系统】北京庞大豪庭汽车销售服务有限公司代运营上线通知，合同编号：CZ2018030803');
		$mailer = new Sys_Mail();
		$result = $mailer->sendMail($to,$subjet,$body);
		var_dump($result);
		
		echo "End.";
		die;
	}
	
	function nhAction()
	{
		$params = $this->_request->getParams();
		
		if(is_numeric($params['value']))
		{
			$val = $params['value'];
			
			$profit_per_day = 100 * ($val / 100) / 365;
			
			$rate_per_day = $profit_per_day / 100;
			
			$money = 100;
			
			for($n=0;$n<365;$n++)
			{
				$money = $money * (1 + $rate_per_day);
			}
			
			$result = $money - 100;
			echo round($result, 4)."%";
		}else{
			echo "Error";
		}
		
		die;
	}
	
	function dcdAction()
	{
		$params = $this->_request->getParams();
		
		$hxb_db = Zend_Registry::get('hxb_db');
		
		if(1 == $params['unique'])
		{
			$unique = 1;
		}else{
			$unique = 0;
		}
		
		if($params['from'])
		{
			$from = $params['from']." 00:00:00";
		}else{
			$from = date("Y-m-d H:i:s", mktime(0,0,0,date("m"),date("d"),date("Y")));
		}
		
		if($params['to'])
		{
			$to = $params['to']." 23:59:59";
		}else{
			$to = date("Y-m-d H:i:s");
		}
		
		$select = $hxb_db->select();
		$select->from("hxb_db.s_leads as l", array("cityName", "factoryName", "count(*) as ct"));
//		$select->joinLeft("dealers.db.t_area as a", "a.");
		$select->where("clientId = ?", "app_huohuaqiche");
		$select->where("createTime >= ?", $from);
		$select->where("createTime <= ?", $to);
		$select->group(array("cityName", "factoryName"));
		$select->order("ct DESC");
		$select->having("ct > 5");
		$rows = $hxb_db->fetchAll($select);
		
		$this->view->data = $rows;
		
		$this->view->title = "懂车帝";
	}
	
	function dcdExportAction()
	{
		$params = $this->_request->getParams();
        $objPHPExcel = new PHPExcel();
		
		$hxb_db = Zend_Registry::get('hxb_db');
		
		if(1 == $params['unique'])
		{
			$unique = 1;
		}else{
			$unique = 0;
		}
		
		if($params['from'])
		{
			$from = $params['from']." 00:00:00";
		}else{
			$from = date("Y-m-d H:i:s", mktime(0,0,0,date("m"),date("d"),date("Y")));
		}
		
		if($params['to'])
		{
			$to = $params['to']." 23:59:59";
		}else{
			$to = date("Y-m-d H:i:s");
		}
		
		$select = $hxb_db->select();
		$select->from("hxb_db.s_leads as l", array("cityName", "factoryName", "count(*) as ct"));
//		$select->joinLeft("dealers.db.t_area as a", "a.");
		$select->where("clientId = ?", "app_huohuaqiche");
		$select->where("createTime >= ?", $from);
		$select->where("createTime <= ?", $to);
		$select->group(array("cityName", "factoryName"));
		$select->order("ct DESC");
		$select->having("ct > 5");
		$result = $hxb_db->fetchAll($select);
		
		if(!empty($result))
        {
            //title
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '城市');
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '厂商');
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '数量');

            $n = 2;
			
            foreach($result as $row)
            {
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$n, $row['cityName']);
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$n, $row['factoryName']);
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$n, $row['ct']);

                $n += 1;
            }
        }
		
		$rd = mt_rand(1000, 9999);
		$folder = "upload/reports/";
        $file_name = "dcd-".date("YmdHis").$rd.".xlsx";
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save($folder.$file_name);
        header("Location: /".$folder.$file_name);

        echo "End.";
        die;
	}
	
	function dtAction()
	{
		$mod = new Db_Joins_ReportsJoins();
		$mod->date_start = '2000-01-01';
		$mod->date_end = '2018-03-21';
		$mod->SalesMain();
		
		die;
	}
	
	function hxbDealersAction()
	{
		$hxb_db = Zend_Registry::get('hxb_db');
		
		$select = $hxb_db->select();
		$select->from("hxb_db.t_hxb_oc_log as l", array());
		$select->joinLeft("dealers_db.t_dealer_hxb as d", "d.id=l.dealerId", array("companyName as dealer_name"));
		$select->joinLeft("dealers_db.t_dealer as d2", "d2.id=l.dealerId", array("companyName as dealer_name_2"));
		$select->joinLeft("dealers_db.t_dealer_group as g", "g.id=d.groupId", array("name as group_name"));
		$select->where("l.date = ?", date("Y-m-d", mktime(1,1,1,date("m"),date("d")-1,date("Y"))));
		$data = $hxb_db->fetchAll($select);
		
		$ext = new Sys_Extensions();
		$ext->FormatArray($data);
		die;
		
		$this->view->data = $data;
	}
	
	function jigeAction()
	{
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$product_id = 2;
		$time_start = "2018-03-01 00:00:00";
		$time_end = "2018-03-28 00:00:00";
		
		$emp_id_arr = array();
			
		$mod_user = new Db_Tables2_User();
		$emp_id_arr = $mod_user->GetMyStaffIds($_SESSION['identity']['userid']);
		
		$select = $dealers_db->select();
		$select->from("b_logs_contract as l", array("value"));
		$select->joinLeft("b_contract as c", "c.contract_id=l.contract_id", array("client_name"));
		$select->joinLeft("b_contract_sku as s", "s.contract_id=l.contract_id", array());
		$select->where("l.ctime >= ?", $time_start);
		$select->where("l.ctime <= ?", $time_end);
		$select->where("s.product_id = ?", $product_id);
		$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
		$select->where("c.contract_sid is not null or contract_sid != ''");
		$rows = $dealers_db->fetchAll($select);
		
		$n = 1;
		
		if(!empty($rows))
		{
			foreach($rows as $row)
			{
				echo $n." - ".$row['client_name']." - ".$row['value']."<br>";
				$n += 1;
			}
		}
		
		die;
	}
	
	function dcdDailyAction()
	{
		$this->view->title = "懂车帝每日线索总量";

        $data = array();
		
		$mod_ext = new Sys_Extensions();
		
		$hxb_db = Zend_Registry::get('hxb_db');
		
		$from = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-7,date("Y")));
		
		$select = $hxb_db->select();
		$select->from("s_leads", array("left(createTime, 10) as ctime", "count(*) as total", "count(distinct phoneNum) as uni"));
		$select->where("clientId = ?", "app_huohuaqiche");
		$select->where("createTime >= ?", $from);
		$select->group("ctime");
		$select->order("createTime DESC");

		$rows = $hxb_db->fetchAll($select);

		if($rows)
		{
			foreach($rows as $row)
			{
				$data[$row['ctime']] = array(
					"date" => $mod_ext->FormatDate($row['ctime']),
					"total" => $row['total'],
					"uni" => $row['uni']
				);
			}
		}

		$select = $hxb_db->select();
		$select->from("t_customer_leads", array("createDate", "count(distinct phoneNum) as uni"));
		$select->where("source = ?", "火花汽车");
		$select->where("createTime >= ?", $from);
		$select->group("createDate");
		$select->order("createDate ASC");
		$rows = $hxb_db->fetchAll($select);
		if($rows)
		{
			foreach($rows as $row)
			{
				if(isset($data[$row['createDate']]))
				{
					$data[$row['createDate']]['given'] = $row['uni'];
				}
			}
		}

		$this->view->data = $data;
		
		$select = $hxb_db->select();
		$select->from("t_customer_leads as l", array("v.cityName as city_name", "v.companyName as dealer_name", "count(distinct phoneNum) as ct"));
		$select->joinLeft("dealers_db.t_dealer as v", "v.id=l.dealerId", array());
		$select->where("l.source = ?", "火花汽车");
		$select->where("l.createTime >= ?", $from);
		$select->group("l.dealerId");
		$select->order("ct DESC");
		$select->limit(50);
		$rows = $hxb_db->fetchAll($select);
		$this->view->data2 = $rows;
	}
	
	function staffTreeAction()
	{
		$hxb_db = Zend_Registry::get('hxb_db');
		
		$select = $hxb_db->select();
		$select->from("auth_emp", array("id", "parentEmpId", "realName"));
		$select->where("id >= ?", 95);
		$rows = $hxb_db->fetchAll($select);
		
		$data = array();
		
		foreach($rows as $row)
		{
			$data[$row['id']] = array(
				"parentEmpId" => $row['parentEmpId'],
				"realName" => $row['realName']
			);
		}
		
		$parent = array();
		$root = array();
		
		foreach($data as $emp_id => $info)
		{
			if(!$info['parentEmpId'])
			{
				$root[$emp_id] = $info['realName'];
			}else{
				if(!isset($parent[$info['parentEmpId']]))
				{
					$parent[$info['parentEmpId']] = array();
				}

				$parent[$info['parentEmpId']] = array(
					"emp_id" => $emp_id,
					"name" => $info['realName']
				);
			}
		}
		
		foreach($root as $root_id => $root_name)
		{
			echo $root_name." -> ";
			
			$emp_id = $root_id;
			
			while(isset($parent[$emp_id]))
			{
				
			}
		}
	}
	
	function apiAction()
	{
		$method = "GET";
		// 请求示例 url 默认请求参数已经做URL编码
		$url = "http://api01.bitspaceman.com:8000/post/autohome?apikey=E2QtQc8W686kp9N22rPE5fr4YErf7Gvtzo4cLEXZIg0UVJfTsjA1OdTrNQGqkteR&";
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_HEADER, true);
		curl_setopt($curl, CURLOPT_ENCODING, "gzip");
		var_dump(curl_exec($curl));
		die;
	}
	
	function copySkuTableAction()
	{
		die;
		$dealers_db = Zend_Registry::get('dealers_db');
		$mod_sku = new Db_Tables2_BSku();
		
		$source = array(1,33);
		
		$target_name = "2018 Q2";
		
		foreach($source as $sid)
		{
			$one = $mod_sku->GetSkuRow($sid);
			
			$set = array();
			
			if($one['product_id'])
			{
				$set['product_id'] = $one['product_id'];
			}
			
			$set['parent_sku_id'] = $target_parent_id;
			
			if($target_name)
			{
				$set['sku_name'] = $target_name;
			}
			
			if($one['unit_name'])
			{
				$set['unit_name'] = $one['unit_name'];
			}
			
			if($one['unit_price'])
			{
				$set['unit_price'] = $one['unit_price'];
			}
			
			$set['status'] = 1;
			
			$dealers_db->insert("b_sku", $set);
			
			$select = $dealers_db->select();
			$select->from("b_sku", array("sku_id"));
			$select->order("sku_id DESC");
			$row = $dealers_db->fetchRow($select);
			
			$parent_sku_id = $row['sku_id'];
		
			$mod_sku = new Db_Tables2_BSku();
			$data = $mod_sku->GetSkuInfo($sid,2,2);
//			$mod_ext = new Sys_Extensions();
//			$mod_ext->FormatArray($data);
//			die;

			foreach($data['data'] as $sku_id => $sku_info)
			{
				$row = $mod_sku->GetSkuRow($sku_id);
				$parent_sku_id2 = $mod_sku->DuplicateRow($row, $parent_sku_id);
				
				if(isset($data['relation'][$sku_id]))
				{
					foreach($data['relation'][$sku_id] as $kid => $kinfo)
					{
						$row = $mod_sku->GetSkuRow($kid);
						$parent_sku_id3 = $mod_sku->DuplicateRow($row, $parent_sku_id2);
						
						if(isset($data['relation'][$kid]))
						{
							foreach($data['relation'][$kid] as $kid2 => $kinfo2)
							{
								$row = $mod_sku->GetSkuRow($kid2);
								$parent_sku_id4 = $mod_sku->DuplicateRow($row, $parent_sku_id3);

								if(isset($data['relation'][$kid2]))
								{
									foreach($data['relation'][$kid2] as $kid3 => $kinfo3)
									{
										$row = $mod_sku->GetSkuRow($kid3);
										$parent_sku_id5 = $mod_sku->DuplicateRow($row, $parent_sku_id4);

										if(isset($data['relation'][$kid3]))
										{
											foreach($data['relation'][$kid3] as $kid4 => $kinfo4)
											{
												$row = $mod_sku->GetSkuRow($kid4);
												$mod_sku->DuplicateRow($row, $parent_sku_id5);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
			
		echo "End";
		die;
	}
	
	function xiaohuiAction()
	{
		$mod_sys = new Sys_Extensions();
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$date_start = "2018-05-01";
		$date_end = "2018-05-31";
		$product_id = 1;
		
		$mod_user = new Db_Tables2_User();
		$emp_id_arr = $mod_user->GetMyStaffIds(99);
		
				$select = $dealers_db->select();
				$select->from("b_contract as c", array("contract_id", "receivable_value", "received_value"));
				$select->joinLeft("b_contract_sku as s", "c.contract_id=s.contract_id", array());
				$select->where("c.sign_date >= ?", $date_start);
				$select->where("c.sign_date <= ?", $date_end);
				$select->where("c.status != ?", 7);
				$select->where("s.product_id = ?", $product_id);
				
				$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
				
				$select->where("c.contract_sid is not null or contract_sid != ''");
				$rows = $dealers_db->fetchAll($select);
				$mod_sys->FormatArray($rows);die;
				if(!empty($rows))
				{
					$contract_id_arr = array();
					
					foreach($rows as $row)
					{
						if(!in_array($row['contract_id'], $contract_id_arr))
						{
							$contract_id_arr[] = $row['contract_id'];
							
							$product_info['qiandan'] += $row['receivable_value'];
							$product_info['qiankuan'] += ($row['receivable_value'] - $row['received_value']);
							$product_info['xinqian'] += 1;
						}
					}
				}
				
				var_dump($product_info);die;
	}

	function testSendServiceNotificationToSalesAction(){
        $mailList = new Db_Tables2_BMailList();
        $mailList->SendServiceNotificationToSales(1180);
    }
	
	function refreshYcCitiesAction()
	{
		$maozhua_wb = Zend_Registry::get("maozhua_wb");
		$dealers_db = Zend_Registry::get("dealers_db");
		
		$select = $maozhua_wb->select();
		$select->from("accounts as a", array("account_id"));
		$select->joinLeft("users as u", "u.user_id=a.user_id", array("city_id"));
		$select->where("a.account_type = ?", 2);
		$rows = $maozhua_wb->fetchAll($select);
		
		foreach($rows as $row)
		{
			if($row['city_id'])
			{
				$dselect = $dealers_db->select();
				$dselect->from("t_area", array("yicheProvinceId", "yicheCityId"));
				$dselect->where("cityId = ?", $row['city_id']);
				$drow = $dealers_db->fetchRow($dselect);
				
				if($drow)
				{
					$data = array(
						"province_id" => $drow['yicheProvinceId'],
						"city_id" => $drow['yicheCityId']
					);
					
					$where = "account_id = ".$row['account_id'];
					$maozhua_wb->update("accounts", $data, $where);
				}
			}
		}
		
		echo "End.";
		die;
	}
	
	function cancelContractAction()
	{
		$params = $this->_request->getParams();
		
		$contract_id = intval($params['contract_id']);
		
		$error = "";
		
		if("LEVEL-3" == $_SESSION['identity']['level'] && $contract_id)
		{
			$dealers_db = Zend_Registry::get("dealers_db");
			
			$select = $dealers_db->select();
			$select->from("b_contract", array("contract_id", "status"));
			$select->where("contract_id = ?", $contract_id);
			$row = $dealers_db->fetchRow($select);
			
			if($row)
			{
				if(7 != $row['status'])
				{
					$set = array("status" => 7);
					$where = "contract_id=".$contract_id;
					$dealers_db->update("b_contract", $set, $where);
					
					$error = "操作成功，合同已作废";
					
				}else{
					$error = "此合同已作废，无需重复操作";
				}
			}else{
				$error = "找不到此合同ID";
			}
		}else{
			$error = "权限不足或参数缺失";
		}
		
		echo $error;
		die;
	}
	
	function getUserAction()
	{
		if(888 != $_SESSION['identity']['userid'])
		{
			echo "Invalid Action.";
			die;
		}
		
		$params = $this->_request->getParams();
		
		if($params['company'])
		{
			$maozhua_wb = Zend_Registry::get("maozhua_wb");

			$select = $maozhua_wb->select();
			$select->from("accounts as a", array("user_name", "pwd", "account_type"));
			$select->joinLeft("users as u", "u.user_id=a.user_id", array());
			$select->where("u.user_name = ?", $params['company']);
			$select->where("a.account_type IN (?)", array(1,2));
			$rows = $maozhua_wb->fetchAll($select);
			
			if(!empty($rows))
			{
				foreach($rows as $row)
				{
					if(1 ==$row['account_type'])
					{
						echo "之家：".$row['user_name']." ".$row['pwd']."<br />";
					}elseif(2 ==$row['account_type'])
					{
						echo "易车：".$row['user_name']." ".$row['pwd']."<br />";
					}
				}
			}
		}
		
		die;
	}
	
	function getContractApprovalUrlAction()
	{
		if(100 != $_SESSION['identity']['userid'])
		{
			echo "Invalid Action.";
			die;
		}
		
		$params = $this->_request->getParams();
		
		if(trim($params['contract_sid']))
		{
			$dealers_db = Zend_Registry::get("dealers_db");

			$select = $dealers_db->select();
			$select->from("b_contract_sku as s", array("mail_token"));
			$select->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array());
			$select->where("c.contract_sid = ?", trim($params['contract_sid']));
			$rows = $dealers_db->fetchAll($select);
			
			if(!empty($rows))
			{
				foreach($rows as $row)
				{
					if($row['mail_token'])
					{
						echo "http://biz.sys.xiongmaocar.com/login/confirm-contract-sku/token/".$row['mail_token']."<br />";
					}
				}
			}else{
				echo "No contract found.";
			}
		}else{
			echo "Parameter Missed";
		}
		
		die;
	}
}
